<?php

class Order extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    //后台列表页
    public function index()
    {
        $act = $this->input->get_post('ajaxdata');
        $result = array('code' => 201, 'message' => '', 'result' => '');

        switch ($act) {
            case 'saveOrder':
                $key = $this->input->get_post('key');
                $order_id = $this->input->get_post('order_id');

                $name = $this->input->get_post('ship_name');
                $tel = $this->input->get_post('ship_tel');
                $address = $this->input->get_post('ship_address');
                $memo = $this->input->get_post('memo');
                $pay_type = (int)$this->input->get_post('pay_method');
                $bank_method = $this->input->get_post('bank_method');
                $online_method = $this->input->get_post('online_method');
                $delivery_time = $this->input->get_post('delivery_time');

                $cartData = json_decode(gCookie('user_cart'), true);

                if ($key) {
                    $keyArr = explode('#', $key);

                    $this->load->library('c_order_save');
                    $sendData = array();
                    $sendData['password'] = $this->lg_user['password'];
                    $sendData['uid'] = $this->lg_user['id'];
                    $sendData['userType'] = $this->lg_user['type'];
                    $sendData['actionType'] = 'submit';
                    $sendData['channelId'] = $this->lg_user['channelId'];

                    $sendData['isPayType'] = $pay_type;
                    if ($pay_type == 1) {
                        $sendData['pay_type_item_id'] = $online_method;
                    } else {
                        $sendData['pay_type_item_id'] = $bank_method;
                    }
                    $sendData['deliveryTime'] = $delivery_time;
                    $sendData['contact'] = $name;
                    $sendData['tel'] = $tel;
                    $sendData['address'] = $address;
                    $sendData['remark'] = $memo;

                    //制造接口请求
                    $this->c_order_save->set_resource_id($order_id);
                    $url = $this->c_order_save->set_call_items($sendData);
                    $param = $this->c_order_save->get_call_items();
                    $rs = $this->restcurl->put($url, $param);

                    if ($rs['ErrorCode'] == 0 && $rs['Success'] == 1 && $rs['ErrorMsg'] == '') {
                        foreach ($keyArr as $v) {
                            if (isset($cartData[$v])) {
                                unset($cartData[$v]);
                            }
                        }
                        sCookie('user_cart', json_encode($cartData));
                        $result['code'] = 200;
                        $result['message'] = '订单提交成功';
                    } else {
                        $result['code'] = 201;
                        $result['message'] = $rs['ErrorMsg'];
                    }
                } else {
                    $result['code'] = 201;
                    $result['message'] = '没有结算的产品';
                }
                break;
            default:
                break;
        }
        echo json_encode($result);
        die;
    }

}
